/*
 * @lc app=leetcode.cn id=383 lang=typescript
 *
 * [383] 赎金信
 */

// @lc code=start
// 通用解法： 将 magazine 理解为供应端，ransomNote 理解为消费端即可
function canConstruct(ransomNote: string, magazine: string): boolean {
    const cache = new Map<string, number>();
    magazine.split("").forEach((i) => {
        cache.set(i, (cache.get(i) || 0) + 1);
    });
    ransomNote.split("").forEach((i) => {
        cache.set(i, (cache.get(i) || 0) - 1);
    });
    for (const iterator of cache.values()) {
        if (iterator < 0) return false;
    }
    return true;
}
// @lc code=end
